﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using System.Data.Common;

namespace GgCarControl
{
    public partial class LoginFrm : Form
    {
        public static Database db = new OracleDatabase("Data Source=DG;User ID=dgrfid;Password=dgrfid;Unicode=True;Load Balance Timeout=1000");

        public LoginFrm()
        {
            InitializeComponent();
            this.AcceptButton = this.button1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string user_name = this.textBox1.Text.ToString().ToUpper();
            string pass_word = this.textBox2.Text.ToString().ToUpper();
            bool bLogin = false;
            string sQX = "";// QX:0 顺序放车账户，1 普通账户，2 放车记录查询账户
            //判断特殊权限
            bLogin = Login_QX(user_name, pass_word, out sQX);
            if (bLogin && sQX == "2")
            {
                DispathJLQueryForm form = new DispathJLQueryForm();
                form.Show();
                this.Hide();
                return;
            }
            if (!bLogin)
            {
                bLogin = Login(user_name, pass_word);
            }
            if (bLogin) //if (user_name == "JKZX" && pass_word == "JKZX" || user_name == "JKZX1" && pass_word == "JKZX1" || user_name == "JKZX2" && pass_word == "JKZX2")
            {
                //这里应该做一个判断，如果登录成功则执行以下代码
                Main frmmain = new Main();
                frmmain.Show();//显示主窗口
                this.Hide();//隐藏当前登录窗口
            }
            else
            {
                MessageBox.Show("密码或用户名错误！");
                return;
            }
        }

        /// <summary>
        /// 特殊权限账户登录判断
        /// </summary>
        /// <returns></returns>
        private bool Login_QX(string yhbh, string pwd, out string qx)
        {
            string sql = "select * from S_RYGL_SENDCARD where YHBH='" + yhbh + "' and klkl='" + pwd + "'";
            DataTable dt = db.ExecuteDataSet(db.GetSqlStringCommand(sql)).Tables[0];
            if (dt.Rows.Count > 0)
            {
                Session.YHBH = dt.Rows[0]["YHBH"].ToString();
                Session.XM = dt.Rows[0]["XM"].ToString();
                Session.QX = dt.Rows[0]["QX"].ToString();
                qx = Session.QX;
                return true;
            }
            qx = "";
            return false;
        }

        private bool Login(string name, string pwd)
        {
            string sql = "select * from S_YH_CZZ where YHBH='" + name + "' and klkl='" + pwd + "'";
            DataTable dt = db.ExecuteDataSet(db.GetSqlStringCommand(sql)).Tables[0];
            if (dt.Rows.Count > 0)
            {
                Session.YHBH = dt.Rows[0]["YHBH"].ToString();
                Session.XM = dt.Rows[0]["XM"].ToString();
                Session.QX = "1";
                return true;
            }
            return false;
        }
    }
}
